Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text

Namespace Enhancements.Core.Logging
    ''' <summary>An enumeration of values that can be used as markers for file name creation substitution.</summary>
    Public NotInheritable Class TokenGenerator

        Private Const __Prefix As System.String = "|"
        Private Const __Suffix As System.String = "|"

        Private __Value As System.String = Nothing
        ''' <summary>The marker to use.</summary>
        Public Property Value() As System.String
            Get
                Return __Value
            End Get
            Private Set(ByVal _value As System.String)
                __Value = _value
            End Set
        End Property

        Private __RegularExpression As Reg.Regex = Nothing
        ''' <summary>The regular expression to use.</summary>
        Public Property RegularExpression() As Reg.Regex
            Get
                Return __RegularExpression
            End Get
            Private Set(ByVal _value As Reg.Regex)
                __RegularExpression = _value
            End Set
        End Property

        ''' <summary>The Guid substitution algorithm.</summary>
        Public Shared ReadOnly Guid As TokenGenerator = New TokenGenerator(0)
        ''' <summary>The date and time substitution algorithm.</summary>
        Public Shared ReadOnly Creation As TokenGenerator = New TokenGenerator(1)

        Private Sub New(ByVal _index As Sys.Int32)
            Dim _value As String = Sys.String.Empty

            _value += __Prefix
            Select Case _index
                Case 0
                    _value += "Guid"
                    RegularExpression = New Reg.Regex("[A-Fa-f0-9]{32}")
                Case 1
                    _value += "Creation"
                    RegularExpression = New Reg.Regex("[0-9]{4}(0[1-9]|10|11|12)(0[1-9]|1[0-9]|2[0-9]|3[0-1])(0[0-9]|1[0-9]|2[0-3])(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])")
            End Select
            _value += __Suffix

            Value = _value
        End Sub

        Friend Shared Function ___Parse(ByVal _value As String) As System.String
            ___Assert.___IsEmptyNot("_value", _value, True)

            Dim _returnValue As System.String = Nothing

            Select Case _value.ToUpperInvariant()
                Case Guid.Value.ToUpperInvariant()
                    _returnValue = "Guid"
                Case Creation.Value.ToUpperInvariant()
                    _returnValue = "Creation"
            End Select

            ___Assert.___IsEmptyNot("_returnValue", _returnValue, True)
            Return _returnValue
        End Function

        Friend Shared Function ___RemoveAllOccurrences(ByVal _value As System.String) As System.String
            ___Assert.___IsNullNot("_value", _value, True)

            Dim _returnValue As System.String = New Tex.StringBuilder(_value).ToString()

            _returnValue = _returnValue.Replace(TokenGenerator.Guid.Value, Sys.String.Empty)
            _returnValue = _returnValue.Replace(TokenGenerator.Creation.Value, Sys.String.Empty)

            Return _returnValue
        End Function

        Friend Shared Function ___AllValuesToArray() As System.String()
            Dim _returnValueList As Gen.List(Of System.String) = New Gen.List(Of System.String)

            _returnValueList.Add(TokenGenerator.Guid.Value)
            _returnValueList.Add(TokenGenerator.Creation.Value)

            Return _returnValueList.ToArray()
        End Function

        Friend Shared Function ___BuildRegularExpressionFromFinalValue(ByVal _value As System.String) As System.String
            ___Assert.___IsNullNot("_value", _value, True)

            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)

            Dim _valueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(_value)
            Do While _valueStringBuilder.Length > 0
                If _valueStringBuilder.ToString().StartsWith(TokenGenerator.Guid.Value, StringComparison.OrdinalIgnoreCase) Then
                    _returnValueStringBuilder.Append(TokenGenerator.Guid.RegularExpression)
                    _valueStringBuilder.Remove(0, TokenGenerator.Guid.Value.Length)
                Else
                    If _valueStringBuilder.ToString().StartsWith(TokenGenerator.Creation.Value, StringComparison.OrdinalIgnoreCase) Then
                        _returnValueStringBuilder.Append(TokenGenerator.Creation.RegularExpression)
                        _valueStringBuilder.Remove(0, TokenGenerator.Creation.Value.Length)
                    Else
                        _returnValueStringBuilder.Append("[" + _valueStringBuilder.Chars(0) + "]{1}")
                        _valueStringBuilder.Remove(0, 1)
                    End If
                End If
            Loop

            Return _returnValueStringBuilder.ToString()
        End Function

        Friend Shared Function ___ReplaceAllOccurrencesWithNewValues(ByVal _value As System.String) As System.String
            ___Assert.___IsNullNot("_value", _value, True)

            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)

            Dim _valueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(_value)
            Do While _valueStringBuilder.Length > 0
                If _valueStringBuilder.ToString().StartsWith(TokenGenerator.Guid.Value, StringComparison.OrdinalIgnoreCase) Then
                    _returnValueStringBuilder.Append(__ReplaceAllOccurrencesWithNewValuesGuidCreate())
                    _valueStringBuilder.Remove(0, TokenGenerator.Guid.Value.Length)
                Else
                    If _valueStringBuilder.ToString().StartsWith(TokenGenerator.Creation.Value, StringComparison.OrdinalIgnoreCase) Then
                        _returnValueStringBuilder.Append(__ReplaceAllOccurrencesWithNewValuesCreationCreate())
                        _valueStringBuilder.Remove(0, TokenGenerator.Creation.Value.Length)
                    Else
                        _returnValueStringBuilder.Append(_valueStringBuilder.Chars(0))
                        _valueStringBuilder.Remove(0, 1)
                    End If
                End If
            Loop

            Return _returnValueStringBuilder.ToString()
        End Function

        Private Shared Function __ReplaceAllOccurrencesWithNewValuesGuidCreate() As System.String
            Return Sys.Guid.NewGuid().ToString("N", Glo.CultureInfo.InvariantCulture)
        End Function

        Private Shared Function __ReplaceAllOccurrencesWithNewValuesCreationCreate() As System.String
            Dim _now As Sys.DateTime = Sys.DateTime.Now

            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)

            _returnValueStringBuilder.Append(_now.Year.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(4, "0"c))
            _returnValueStringBuilder.Append(_now.Month.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(2, "0"c))
            _returnValueStringBuilder.Append(_now.Day.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(2, "0"c))
            _returnValueStringBuilder.Append(_now.Hour.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(2, "0"c))
            _returnValueStringBuilder.Append(_now.Minute.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(2, "0"c))
            _returnValueStringBuilder.Append(_now.Second.ToString(Glo.CultureInfo.InvariantCulture).PadLeft(2, "0"c))

            Return _returnValueStringBuilder.ToString()
        End Function

    End Class
End Namespace
